package com.svms.save.services;

import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.svms.save.entitys.SvdaWzxx;
import com.svms.save.entitys.SvdaWzxxExample;
import com.svms.save.entitys.SvdaYhb;
import com.svms.save.mapper.SvdaWzxxMapper;
import com.svms.save.utils.SysCode;
import com.svms.save.vo.Page;

/**
 * 位置信息管理
 * @author xuyang
 *
 */
public abstract class SvdaWzxxService extends BaseService{
	@Autowired
	private SvdaWzxxMapper wzxxMapper;
	/**
	 * 保存
	 * @param wzxx
	 * @return
	 */
	public int doSave(SvdaWzxx wzxx,SvdaYhb yh) {
		int r = 0;
		super.setEntityDefVal(yh, wzxx);
		if(null == wzxx.getObjectid()) {
			r = wzxxMapper.insert(wzxx);
			wzxx.setCzbs(SysCode.CZBS_ADD);
		}else {
			r = wzxxMapper.updateByPrimaryKeyWithBLOBs(wzxx);
			wzxx.setCzbs(SysCode.CZBS_UPDATE);
		}
		return r;
	}
	/**
	 * 根据主键获取详情
	 * @param objectid
	 * @return
	 */
	public SvdaWzxx findById(Long objectid) {
		return wzxxMapper.selectByPrimaryKey(objectid);
	}
	/**
	 * 分页查询
	 * @param page
	 * @param wzxx
	 * @return
	 */
	public Page<SvdaWzxx> queryPage(Page<SvdaWzxx> page,SvdaWzxx wzxx){
		if(null != wzxx) {
			SvdaWzxxExample ex = new SvdaWzxxExample();
			SvdaWzxxExample.Criteria cri = ex.createCriteria();
			if(StringUtils.isNotBlank(wzxx.getWzmc())) {
				cri.andWzmcLike("%"+wzxx.getWzmc()+"%");
			}
			if(StringUtils.isNotBlank(wzxx.getWzbh())) {
				cri.andWzbhEqualTo(wzxx.getWzbh());
			}
			cri.andCzbsNotEqualTo("3");
			ex.setOrderByClause("czsj desc");
            long conut = wzxxMapper.countByExample(ex);
            page.setTotail(conut);
            ex.setLimit(page.getPageSize());
            ex.setOffset(page.getOffset());
            List<SvdaWzxx> list = wzxxMapper.selectByExampleWithBLOBs(ex);
            page.setResult(list);
		}
		return page;
	}
	/**
	 * 获取全部位置信息
	 * @return
	 */
	public List<SvdaWzxx> getAllList(){
		SvdaWzxxExample ex = new SvdaWzxxExample();
		SvdaWzxxExample.Criteria cri = ex.createCriteria();
		cri.andCzbsNotEqualTo("3");
		ex.setOrderByClause("czsj desc");
        List<SvdaWzxx> list = wzxxMapper.selectByExample(ex);
        return list;
	}
	/**
	 * 校验编号规则是否通过
	 * @param wzbh
	 * @return
	 */
	public SvdaWzxx validateWzbh(String wzbh) {
		if(StringUtils.isNotBlank(wzbh)) {
			SvdaWzxxExample ex = new SvdaWzxxExample();
			SvdaWzxxExample.Criteria cri = ex.createCriteria();
			cri.andWzbhEqualTo(wzbh);
			cri.andCzbsNotEqualTo("3");
			List<SvdaWzxx> list = wzxxMapper.selectByExample(ex);
			if(list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}
}
